package user

import org.apache.shiro.SecurityUtils;
import institutional.Course;
import inscription.InscriptionCourse;

import org.springframework.dao.DataIntegrityViolationException

/**
 * StudentController
 * A controller class handles incoming web requests and performs actions such as redirects, rendering views and so on.
 */
class StudentController {
	def studentService
	def inscriptionService
	def securityService
	
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	
    def index() {
		
			def user = SecurityUtils.getSubject()
			
			if (user.hasRole(securityService.ADMIN_ROLE))			
		   		redirect(action: "list", params: params)
    		else {
				redirect(action: "show", id: Student.findByStudentId(user.principal).id)
				
			}
				
	}

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [studentInstanceList: Student.list(params), studentInstanceTotal: Student.count()]
    }

    def create() {
		[studentInstance: studentService.create(params)]
	}

    def save() {
		def studentInstance = studentService.create(params)
        if (!studentInstance.save(flush: true)) {
			print params.majors
			render(view: "create", model: [studentInstance: studentInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'student.label', default: 'Student'), studentInstance.id])
        redirect(action: "show", id: studentInstance.id)
    }

    def show() {
        def studentInstance = Student.get(params.id)
        if (!studentInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'student.label', default: 'Student'), params.id])
            redirect(action: "list")
            return
        }

        [studentInstance: studentInstance]
    }

    def edit() {
        def studentInstance = Student.get(params.id)
        if (!studentInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'student.label', default: 'Student'), params.id])
            redirect(action: "list")
            return
        }
		[studentInstance: studentInstance]
    }

    def update() {
        def studentInstance = Student.get(params.id)
        if (!studentInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'student.label', default: 'Student'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (studentInstance.version > version) {
                studentInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'student.label', default: 'Student')] as Object[],
                          "Another user has updated this Student while you were editing")
                render(view: "edit", model: [studentInstance: studentInstance])
                return
            }
        }

        studentInstance.properties = params

        if (!studentInstance.save(flush: true)) {
            render(view: "edit", model: [studentInstance: studentInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'student.label', default: 'Student'), studentInstance.id])
        redirect(action: "show", id: studentInstance.id)
    }

    def delete() {
        def studentInstance = Student.get(params.id)
        if (!studentInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'student.label', default: 'Student'), params.id])
            redirect(action: "list")
            return
        }

        try {
            studentInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'student.label', default: 'Student'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'student.label', default: 'Student'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
	
	

	
	def listEnroledCourses() {
		def studentInstance = Student.get(params.id)
		def semester = inscriptionService.currentSemester
		
		if (!studentInstance) {
			render(uri: "home/index")
			return
		}
		
		def insciptionCourse = InscriptionCourse.findAll("from InscriptionCourse as ic where ic.semester=:semester and ic.student=:student",
						[semester: semester, student: studentInstance])
		
		[inscriptionCourseInstanceList: insciptionCourse, inscriptionCourseInstanceTotal: insciptionCourse.size()]
	}
	
	
	
}
